﻿using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Text;
using System.Collections.Generic;
using Microsoft.BizTalk.Component.Interop;
using AppFx.BizTalk.PipelineComponents;

namespace AppFx.BizTalk.Tests.PipelineComponents
{
    /// <summary>
    ///Tests the property bag helper
    ///</summary>
    [TestClass()]
    public class PropertyBagHelperTest
    {       
        /// <summary>
        ///A test for full (IPropertyBag, string)
        ///</summary>
        [TestMethod()]
        public void FullTest()
        {
            const string PropertyName = "Test";
            const string expected = "TestValue";

            MockPropertyBag pb = new MockPropertyBag();

            PropertyBagHelper.Write(pb, PropertyName, expected);
            string actual = (string)PropertyBagHelper.Read(pb, PropertyName);
            Assert.IsTrue(actual == expected, "The prop value is not as expected");

            actual = (string)PropertyBagHelper.Read(pb, "UnknownProperty");
            Assert.IsNull(actual, "The actual should be null");

        }
        /// <summary>
        /// Code path for an error saving pb
        /// </summary>
        [TestMethod]
        [ExpectedException(typeof(ApplicationException))]
        public void SaveErrorTest()
        {
            MockPropertyBag pb = new MockPropertyBag();
            pb.ThrowException = true;
            object val = new object();
            pb.Write("", ref val);

        }
        /// <summary>
        /// Code path for an error loading pb
        /// </summary>
        [TestMethod]
        [ExpectedException(typeof(ApplicationException))]
        public void LoadErrorTest()
        {
            MockPropertyBag pb = new MockPropertyBag();
            pb.ThrowException = true;
            object val = new object();
            pb.Read("", out val, 0);
        }
    }
    /// <summary>
    /// Allows us to check the property bag for items
    /// </summary>
    public class MockPropertyBag : IPropertyBag
    {
        /// <summary>
        /// tells mock to throw an exception
        /// </summary>
        public bool ThrowException;
        Dictionary<string, object> _PropertyBag = new Dictionary<string, object>();

        #region IPropertyBag Members
        /// <summary>
        /// Implements read
        /// </summary>
        /// <param name="propName"></param>
        /// <param name="ptrVar"></param>
        /// <param name="errorLog"></param>
        public void Read(string propName, out object ptrVar, int errorLog)
        {
            if (ThrowException)
                throw new ApplicationException("Exception");

            ptrVar = this._PropertyBag[propName];
        }
        /// <summary>
        /// implements write
        /// </summary>
        /// <param name="propName"></param>
        /// <param name="ptrVar"></param>
        public void Write(string propName, ref object ptrVar)
        {
            if (ThrowException)
                throw new ApplicationException("Exception");

            if (this._PropertyBag.ContainsKey(propName))
                this._PropertyBag.Remove(propName);

            this._PropertyBag.Add(propName, ptrVar);
        }

        #endregion
    }
}


